package com.dailyblue.java.spring.cloud.example.sentinel.customer.controller;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.dailyblue.java.spring.cloud.example.commons.util.JsonResult;
import com.dailyblue.java.spring.cloud.example.commons.util.ResultTool;
import com.dailyblue.java.spring.cloud.example.sentinel.customer.feign.ProductFeign;
import com.dailyblue.java.spring.cloud.example.sentinel.customer.handler.MyBlockHandler;
import com.dailyblue.java.spring.cloud.example.sentinel.customer.handler.MyFallbackHandler;
import com.dailyblue.java.spring.cloud.example.sentinel.customer.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/customer")
@Slf4j
public class CustomerController {
    @Resource
    private CustomerService customerService;

    @GetMapping("/a")
    public JsonResult a() {
        log.info("调用了a方法");
        return customerService.a();
    }

    @GetMapping("/b")
    public JsonResult b() {
        log.info("调用了b方法");
        return customerService.b();
    }

    @GetMapping("/c")
    public JsonResult c() {
        log.info("调用了c方法");
        return customerService.c();
    }

    @GetMapping("/d")
    public JsonResult d() {
        log.info("调用了d方法");
        return customerService.c();
    }

    @GetMapping("/e")
    @SentinelResource(value = "messageE", fallback = "error")
    public JsonResult e() {
        int a = (int) (Math.random() * 3);
        int result = 3 / a;
        log.info("3/{}={}", a, result);
        return customerService.b();
    }

    public JsonResult error(Throwable e) {
        return ResultTool.success("你的代码出现了错误，我是错误处理方案，错误是：" + e.getMessage());
    }

    @GetMapping("/f")
    @SentinelResource(value = "messageF", blockHandler = "blockFmethod")
    public JsonResult f(String name, Integer age) {
        log.info("传递进来的参数是:{},{}", name, age);
        return customerService.b();
    }

    @GetMapping("/g")
    @SentinelResource(value = "messageG",
            blockHandlerClass = MyBlockHandler.class, blockHandler = "blockHandler",
            fallback = "fallback", fallbackClass = MyFallbackHandler.class)
    public JsonResult g(String type) {
        log.info("传递进来的参数是:{}", type);
        return customerService.b();
    }

    public JsonResult blockFmethod(String name, Integer age, BlockException e) {
        log.info("进入到了兜底方案");
        return ResultTool.success("我是兜底方案返回的结果");
    }

    @GetMapping("/c1/{index}")
    public JsonResult c1(@PathVariable Integer index) {
        return customerService.c1(index);
    }
}
